package com.lbb.springboot.util;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author likanghong
 */
public class StrUtils {

    public static void main(String[] rags) {
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
        System.out.println(getProbability(0, 100));
    }


    public static int getProbability(int start, int end) {
        Random random = new Random();
        int num = random.nextInt(end - start + 1) + start;
        return num;
    }

    public static BigDecimal getOpRed() {
//        5-8元概率80%     8-10概率20%
        int probability = getProbability(1, 100);
        int red = 0;
        if (probability > 0 && probability <= 80) {
            red = getProbability(5, 8);
        } else {
            red = getProbability(8, 10);
        }
        return new BigDecimal(red);
    }

    public  static  BigDecimal getRandomRed(int total,int num){
        //得到中间数
        int curr=num/2;
//        BigDecimal redNum=;
        return  null;
    }


    public static BigDecimal getGpsRed() {
//    15-20元概率70%   20-25元概率20%   25-30元概率10%
        int probability = getProbability(1, 100);
        int red = 0;
        if (probability > 0 && probability <= 70) {
            red = getProbability(15, 20);
        } else if (probability > 70 && probability <= 90) {
            red = getProbability(20, 25);
        } else {
            red = getProbability(25, 30);
        }
        return new BigDecimal(red);
    }

    public static BigDecimal getBalance(BigDecimal balance) {

//        充值满100赠送6元（充值100得106）
//        充值满500赠送36元（充值500得538）
//        充值满1000赠送88元（充值1000得1088）
//        充值满5000赠送520元（充值5000得5520）
//        充值满10000赠送1268元（充值10000得11268）
//        充值满20000赠送2888元（充值20000得22888）

        if (balance.compareTo(new BigDecimal(20000)) >= 0) {
            return balance.add(new BigDecimal(2888));
        } else if (balance.compareTo(new BigDecimal(10000)) >= 0) {
            return balance.add(new BigDecimal(1268));
        } else if (balance.compareTo(new BigDecimal(5000)) >= 0) {
            return balance.add(new BigDecimal(520));
        } else if (balance.compareTo(new BigDecimal(1000)) >= 0) {
            return balance.add(new BigDecimal(88));
        } else if (balance.compareTo(new BigDecimal(500)) >= 0) {
            return balance.add(new BigDecimal(38));
        } else if (balance.compareTo(new BigDecimal(100)) >= 0) {
            return balance.add(new BigDecimal(6));
        }


        return balance;
    }

    public static String getTestProd(String url) {
        if (url.contains("uukache")) {
            return "生产";
        } else {
            return "测试";
        }
    }

    /**
     * 解析地址
     *
     * @param address
     * @return
     * @author lin
     */
    public static List<Map<String, String>> addressResolution(String address) {
        String regex = "(?<province>[^省]+省|.+自治区)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|.+区)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        String province = null, city = null, county = null, town = null, village = null;
        List<Map<String, String>> table = new ArrayList<Map<String, String>>();
        Map<String, String> row = null;
        while (m.find()) {
            row = new LinkedHashMap<String, String>();
            province = m.group("province");
            row.put("province", province == null ? "" : province.trim());
            city = m.group("city");
            row.put("city", city == null ? "" : city.trim());
            county = m.group("county");
            row.put("county", county == null ? "" : county.trim());
            town = m.group("town");
            row.put("town", town == null ? "" : town.trim());
            village = m.group("village");
            row.put("village", village == null ? "" : village.trim());
            table.add(row);
        }
        return table;
    }


    public static boolean getIsStart(String str) {
        char[] chars = str.toCharArray();
        if (chars[chars.length - 1] >= 48 && chars[chars.length - 1] <= 57) {
            return false;
        }
        return true;
    }


    public static Integer getNum(String str) {
        Integer start = null;
        int index = 0;
        char[] chars = str.toCharArray();

        String substring = null;
        if (chars[chars.length - 1] >= 48 && chars[chars.length - 1] <= 57) {
            for (int i = chars.length - 1; i >= 0; i--) {
                if (chars[i] >= 48 && chars[i] <= 57) {
                    continue;
                } else {
                    index = i + 1;
                    break;
                }
            }
            substring = str.substring(index, str.length());

//            substring = str.substring(0, index);
        } else {
            for (int i = 0; i <= chars.length - 1; i++) {
                if (chars[i] >= 48 && chars[i] <= 57) {
                    continue;
                } else {
                    index = i + 1;
                    break;
                }
            }
//            substring = str.substring(index-1, str.length());
            substring = str.substring(0, index - 1);
        }
        System.out.println("substring>>>>>>>>" + substring);
        return Integer.parseInt(substring);
    }


    public static String getStr(String str) {
        Integer start = null;
        int index = 0;
        char[] chars = str.toCharArray();

        String substring = null;
        if (chars[chars.length - 1] >= 48 && chars[chars.length - 1] <= 57) {
            for (int i = chars.length - 1; i >= 0; i--) {
                if (chars[i] >= 48 && chars[i] <= 57) {
                    continue;
                } else {
                    index = i + 1;
                    break;
                }
            }
            substring = str.substring(0, index);
        } else {
            for (int i = 0; i <= chars.length - 1; i++) {
                if (chars[i] >= 48 && chars[i] <= 57) {
                    continue;
                } else {
                    index = i + 1;
                    break;
                }
            }
            substring = str.substring(index - 1, str.length());
        }
        return substring;
    }

}
